
const state = () => ({
    panes: [],
    activePane: "",
    isHideComplete: false
});

function getShowList(isHideComplete, list) {
    if (isHideComplete) {
        return list.filter(val => !val.isComplete);
    }
    return list;
}

// getters
const getters = {}

// actions
const actions = {
    async getHideFlag({ commit }) {
        const hideFlag = await window.electronAPI.getUserData('isHideComplete');
        commit('setHideComplete', hideFlag);
        return hideFlag;
    },
    async getTaskData({ dispatch, commit }) {
        const taskMap = await window.electronAPI.getTask();
        const hideFlag = await dispatch('getHideFlag');
        const initialPanes = [];
        taskMap.forEach((value, board) => {
            const showList = getShowList(hideFlag, value.list);
            const panel = {
                title: `${value.title.prefix} ${value.title.message} ${value.title.suffix}`,
                content: {list: showList, board},
                key: board,
                closable: false,
            };
            initialPanes.push(panel);
        });
        commit('setPanes', [initialPanes, taskMap]);
    }
}

// mutations
const mutations = {
    setPanes(state, [initialPanes, taskMap]) {
        state.panes = initialPanes;
        if (!taskMap.has(state.activePane)) {
            state.activePane = initialPanes && initialPanes[0].key;
        }
    },
    setActivePane (state, activePane) {
        state.activePane = activePane;
    },
    setHideComplete(state, flag) {
        state.isHideComplete = flag;
        window.electronAPI.setUserData('isHideComplete', flag);
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}